/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.miracleteam.miracle.dao.daoImpl;

import com.miracleteam.miracle.dao.CauHoiChoCuaHangDAO;
import com.miracleteam.miracle.entities.Cauhoichocuahang;
import com.miracleteam.miracle.utils.HibernateUtil;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author DangKhoa
 */
public class CauHoiChoCuaHangDAOImpl implements CauHoiChoCuaHangDAO{
   Logger logger= Logger.getLogger(TinhThanhDAOImpl.class);
    public String PhatSinhMaCauHoi() {
          String kq="Q-";
        Date date = new Date();
        kq+=String.valueOf(date.getDay());
        kq+=String.valueOf(date.getMonth());
        kq+=String.valueOf(date.getYear());
        kq+="-";
        kq+=String.valueOf(date.getHours());
        kq+=String.valueOf(date.getMinutes());
        kq+=String.valueOf(date.getSeconds());
        
        return kq;
    }

    public List LayDSCauHoi(String maGH) {
         List ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select ch from Cauhoichocuahang ch where ch.daXoa=0 and  ch.gianhang.maGianHang =:maGH order by ch.thoiGianGuiCauHoi DESC";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", maGH);
            query.setFirstResult(0);
            query.setMaxResults(9);
            ds = query.list();
        } catch (Exception e) {
             logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        return ds;
   
    }
    public int ThemCauHoi(Cauhoichocuahang ch)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (ch== null) {
            return 0;
        }
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(ch);
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            logger.error(ex.getMessage());
            System.err.println(ex);
        } finally {
            session.close();
        }
        return 1;

    }
    public Cauhoichocuahang LayCauHoi(String ma)
    {
        Cauhoichocuahang ch = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select ch from Cauhoichocuahang ch where ch.daXoa=0 and  ch.maCauHoiChoCuaHang =:maCH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maCH", ma);
            ch = (Cauhoichocuahang)query.uniqueResult();
        } catch (Exception e) {
             logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        return ch;
    }
    
}
